﻿<template>
	<div class="customer-container">
		<el-dialog v-model="isShowDialog" :width="800" draggable="" :close-on-click-modal="false">
			<template #header>
				<div style="color: #fff">
					<span>{{ props.title }}</span>
				</div>
			</template>

			<el-form :model="ruleForm" ref="ruleFormRef" label-width="auto" :rules="rules">
				<el-row :gutter="35">
					<el-form-item v-show="false">
						<el-input v-model="ruleForm.id" />
					</el-form-item>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="客户名称" prop="cusName">
							<el-input v-model="ruleForm.cusName" placeholder="请输入客户名称" maxlength="64" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
					</el-col>

					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="数量" prop="quantity">
							<el-input-number  v-model="ruleForm.quantity" placeholder="请输入数量" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
					</el-col>


					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="类型" prop="type">
							<!-- <el-input-number v-model="ruleForm.type" placeholder="请输入客户类型" clearable /> -->
								<el-select v-model="ruleForm.type" placeholder="请输入客户类型" clearable>
									<el-option label="审计" :value="1" />
									<el-option label="评估" :value="2" />
								</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="点数" prop="money">
							<el-input-number v-model="ruleForm.money" placeholder="请输入金额"  show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="资产额(万)" prop="propertyMoney">
							<el-input-number v-model="ruleForm.propertyMoney" placeholder="请输入金额（万元）"  show-word-limit clearable />
						</el-form-item>
					</el-col>


					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="是否出报告" prop="isReport">
							<el-switch v-model="ruleForm.isReport" active-text="是" inactive-text="否" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="报告日期" prop="reportTime">
							<el-date-picker v-model="ruleForm.reportTime" type="date" placeholder="出报告日期" />
						</el-form-item>
					</el-col>

					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="是否收款" prop="isCollected">
							<el-switch v-model="ruleForm.isCollected" active-text="是" inactive-text="否" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="收款日期" prop="collectedTime">
							<el-date-picker v-model="ruleForm.collectedTime" type="date" placeholder="收款日期" />
						</el-form-item>
					</el-col>

					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="渠道对接" prop="sourceChanne">
						<el-select v-model="ruleForm.sourceChanne"  value-key="id" clearable placeholder="请选择渠道对接" collapse-tags collapse-tags-tooltip class="w100" filterable>
							<el-option v-for="item in resData.sourceChanneList" :key="item.id" :label="item.name" :value="item.id" />
						</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="渠道名称" prop="sourceChanneName">
							<el-input v-model="ruleForm.sourceChanneName" placeholder="请输入渠道名称" maxlength="255" show-word-limit clearable />
						</el-form-item>
					</el-col>

					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="备注" prop="remark">
							<el-input type="textarea" v-model="ruleForm.remark" placeholder="请输入备注"  rows="3" maxlength="600" show-word-limit clearable />
						</el-form-item>
					</el-col>

					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="凭证备注" prop="voucherRemark">
							<el-input type="textarea" v-model="ruleForm.voucherRemark" placeholder="请输入凭证备注"  rows="3" maxlength="600" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<!-- <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						 <el-form-item label="凭证图片" prop="voucherImg">
							<el-upload ref="uploadRef" class="avatar-uploader" :showFileList="true" :autoUpload="false" accept=".jpg,.png,.svg" action="" :limit="3" :onChange="handleUploadChange">
								<img v-if="ruleForm.voucherImg" :src="ruleForm.voucherImg" class="avatar"  width="100" height="100"/>
								<SvgIcon v-else class="avatar-uploader-icon" name="ele-Plus" :size="40" />
							</el-upload>
						</el-form-item> 
					</el-col> -->
				</el-row>
			</el-form>


			<template #footer>
				<span class="dialog-footer">
					<el-button @click="cancel">取 消</el-button>
					<el-button type="primary" @click="submit">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>
<style lang="scss" scoped>
:deep(.el-select),
:deep(.el-input-number) {
	width: 100%;
}
</style>
<script lang="ts" setup>
	import { ref,onMounted } from "vue";
	import { ElMessage , UploadInstance} from "element-plus";
	import type { FormRules } from "element-plus";
  	import { formatDate } from '/@/utils/formatTime';
	import { addCustomer, updateCustomer, detailCustomer } from "/@/api/main/customer";
	import { getListCustomerChannel } from '/@/api/main/customerChannel';
	import Editor from '/@/components/editor/index.vue';
	import { SysFileApi } from '/@/api-services/api';
	import { getAPI } from '/@/utils/axios-utils';
	//父级传递来的参数
	var props = defineProps({
		title: {
		type: String,
		default: "",
	},
	});

	const resData = ref({
	  sourceChanneList: [] as any
	});

	//父级传递来的函数，用于回调
	const emit = defineEmits(["reloadTable"]);
	const ruleFormRef = ref();
	const isShowDialog = ref(false);
	const ruleForm = ref<any>({});
	const uploadRef = ref<UploadInstance>();
	//自行添加其他规则
	const rules = ref<FormRules>({
		cusName: [{required: true, message: '请输入客户名称！', trigger: 'blur',},],
		type: [{required: true, message: '请输入客户类型！', trigger: 'blur',},],
		//money: [{required: true, message: '请输入金额！', trigger: 'blur',},],
	});

	// 页面加载时
	onMounted( async() => {
		var res = await getListCustomerChannel();
		resData.value.sourceChanneList = res.data.result ?? [];
	});

	// 通过onChange方法获得文件列表
	const handleUploadChange = async (file: any) => {
		uploadRef.value!.clearFiles();
		//ruleForm.value.voucherImg = URL.createObjectURL(file.raw); // 显示预览logo
		var resimg = await getAPI(SysFileApi).apiSysFileUploadFilePostForm(file.raw, "相关文件", true, undefined);
		if (resimg.data.type == 'success' && resimg.data.result != null) {
			ruleForm.value.voucherImg =resimg.data.result.url;
		} else {
			ElMessage.error('上传失败！');
		}
	};

	// 打开弹窗
	const openDialog = async (row: any) => {
		// ruleForm.value = JSON.parse(JSON.stringify(row));
		// 改用detail获取最新数据来编辑
		let rowData = JSON.parse(JSON.stringify(row));
		if (rowData.id)
			ruleForm.value = (await detailCustomer(rowData.id)).data.result;
		else
			ruleForm.value = rowData;
		isShowDialog.value = true;
	};

	// 关闭弹窗
	const closeDialog = () => {
		emit("reloadTable");
		isShowDialog.value = false;
	};

	// 取消
	const cancel = () => {
		isShowDialog.value = false;
	};

	// 提交
	const submit = async () => {
		ruleFormRef.value.validate(async (isValid: boolean, fields?: any) => {
			if (isValid) {
				let values = ruleForm.value;
				if (ruleForm.value.id == undefined || ruleForm.value.id == null || ruleForm.value.id == "" || ruleForm.value.id == 0) {
					await addCustomer(values);
				} else {
					await updateCustomer(values);
				}
				closeDialog();
			} else {
				ElMessage({
					message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
					type: "error",
				});
			}
		});
	};






	//将属性或者函数暴露给父组件
	defineExpose({ openDialog });
</script>




